package cn.jeeweb.modules.server.service.impl;

import cn.jeeweb.core.common.service.impl.CommonServiceImpl;
import cn.jeeweb.core.utils.MessageUtils;
import cn.jeeweb.core.utils.upload.exception.FileNameLengthLimitExceededException;
import cn.jeeweb.core.utils.upload.exception.InvalidExtensionException;
import cn.jeeweb.modules.server.mapper.OrderMapper;
import cn.jeeweb.modules.server.entity.Order;
import cn.jeeweb.modules.server.service.IOrderService;
import cn.jeeweb.modules.sys.entity.Attachment;
import cn.jeeweb.modules.sys.service.IAttachmentService;
import cn.jeeweb.modules.sys.utils.UserUtils;
import org.apache.commons.fileupload.FileUploadBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**   
 * @Title: 订单
 * @Description: 订单
 * @author zhuht
 * @date 2018-08-09 20:21:54
 * @version V1.0   
 *
 */
@Transactional
@Service("orderService")
public class OrderServiceImpl  extends CommonServiceImpl<OrderMapper,Order> implements  IOrderService {
	@Autowired
	private IAttachmentService attachmentService;

	public void updateOrder(Order order, MultipartFile[] files, HttpServletRequest request){
		List<Attachment> attachmentList = new ArrayList<Attachment>();
		for(MultipartFile file:files){
			try {
				Attachment attachment = attachmentService.upload(request, file);
				attachmentList.add(attachment);
			} catch (IOException e) {
				throw new RuntimeException("图片上传异常");
			} catch (InvalidExtensionException e) {
				throw new RuntimeException("图片上传异常");
			} catch (FileUploadBase.FileSizeLimitExceededException e) {
				throw new RuntimeException("图片上传异常");
			} catch (FileNameLengthLimitExceededException e) {
				throw new RuntimeException("图片上传异常");
			}
		}
		if(attachmentList.size()>0){
			order.setImgpath1(attachmentList.get(0).getFilepath());
		}
		if(attachmentList.size()>1){
			order.setImgpath2(attachmentList.get(1).getFilepath());
		}
		if(attachmentList.size()>2){
			order.setImgpath3(attachmentList.get(2).getFilepath());
		}
		if(attachmentList.size()>3){
			order.setImgpath4(attachmentList.get(3).getFilepath());
		}

		order.setCreateName(UserUtils.getUser().getRealname());

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		order.setDatetime(sdf.format(new Date()));
		this.insert(order);
	}
}
